package cn.itrigger.mojos;

import cn.itrigger.config.VersionConfig;
import cn.itrigger.config.VersionLevelEnum;
import cn.itrigger.publish.ProdVersionPublish;
import cn.itrigger.publish.VersionPublish;
import cn.itrigger.utils.GitOperatorUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

@Mojo(name = "prod")
public class ItriggerProdMojo extends ItriggerReleaseMojo {

    //打标签，默认不打
    @Parameter(property = "tag", defaultValue = "false")
    private String tag;
    @Parameter(property = "prodSubffix", defaultValue = "")
    private String prodSubffix;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        if (BooleanUtils.toBoolean(help)) {
            help();
            return;
        }
        System.out.println("开始发布PROD版本");
        //所有操作基于prod分支
        GitOperatorUtils.checkout(prodBranch);
        //代码更新
        GitOperatorUtils.pull(prodBranch);

        GitOperatorUtils.reset();


        String[] diff = GitOperatorUtils.diff();
        if (null == diff || diff.length == 0){
            VersionConfig.VersionConfigBuilder builder = new VersionConfig.VersionConfigBuilder();
            VersionConfig config = null;
            try {
                config = builder.setNexusUrl(nexusUrl).setGitUrl(gitUrl).setEnv(env)
                        .setUatBranch(uatBranch).setTag(tag).setProject(project).setDailyBranch(dailyBranch)
                        .setVersionLevel(VersionLevelEnum.index(Integer.parseInt(versionLevel) - 1)).setProdBranch(prodBranch)
                        .setDailySubffix(dailySubffix).setUatSubffix(uatSubffix).setProdSubffix(prodSubffix)
                        .setUserName(userName).setUserEmail(userEmail)
                        .setName(name).setPassword(password).setFixVersionNumber(fixVersionNumber)
                        .setModule(module).setUpdateVersion(updateVersion).setDeploy(deploy).hasInstall(hasInstall)
                        .uatIsRelease(uatIsRelease).updateDailyVersion(updateDailyVersion)
                        .build();
            }catch (Exception e) {
                e.printStackTrace();
            }

            VersionPublish publish = new ProdVersionPublish(config);
            publish.publish();

        }else {
            System.err.println("本地代码有修改，无法发布版本");
        }
    }

    @Override
    public void validate() throws MojoExecutionException {
        String version = project.getVersion();
        if (version.endsWith("-SNAPSHOT")) {
            throw new MojoExecutionException("不能直接从daily环境跨到prod环境，version="+version);
        }

        //如果不是固定版本号并且后缀不是u
        //
        if (("3".equals(versionLevel) && !version.endsWith("u")) && StringUtils.isBlank(fixVersionNumber)){
            throw new MojoExecutionException("请使用hotfix级别进行版本发布，version="+version);
        }
    }
}
